﻿// Collapse or Expand all nodes/
// param: treeId - The id of the treeView to perform the operation
// param: imgUrl - The Collapse or Expand image url, if send the Collapse image, then all nodes will collapse
function ChnageTreeViewStatus(treeId, imgUrl) {

    // Get all nodes ids that have children
    var parentIds = GetAllParentIds(treeId);

    for (var i = 0; i < parentIds.length; i++) {

        // Get the iamge of each node 
        var parentImg = GetParentImg(treeId, parentIds[i]);

        // If the mode image is the same like imgUrl, we need to toggle
        if (parentImg.src.indexOf(imgUrl) > -1) {

            // Get the node object
            var parent = GetParentObject(treeId, parentIds[i]);
            // Get the child node object (to send as parameter for ToggleNode function)
            var childe = document.getElementById(GetChildeId(treeId, parentIds[i]));
            // Get the TreeView javascript object
            var obj = eval(treeId + "_Data");

            // Perform the ToggleNode function
            TreeView_ToggleNode(obj, treeId[i], parent, ' ', childe);
        }
    }
}

function GetParentImg(treeId, nodeId) {
    var node = GetParentObject(treeId, nodeId);
    var imgNode = node.getElementsByTagName('img');

    return imgNode.length > 0 ? imgNode[0] : null;
}

function GetParentObject(treeId, nodeId) {
    var id = treeId + 'n' + nodeId;
    return document.getElementById(id);
}

function GetChildeId(treeId, NodeId) {
    return treeId + 'n' + NodeId + 'Nodes';
}

function GetAllParentIds(treeId) {
    var aElements = document.getElementById(treeId).getElementsByTagName('a');
    var parentIds = new Array();
    var j = 0;

    for (var i = 0; i < aElements.length; i++) {
        if (aElements[i].id.indexOf('Nodes') < 0 && aElements[i].id.indexOf(treeId + 'n') > -1) {
            parentIds[j] = aElements[i].id.substr(treeId.length + 1);
            j++;
        }
    }
    return parentIds;
}